package com.renwei.yunlong.activity.work;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.ServiceSelectAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.OwnerContentsBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 服务目录 - - 页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class ServiceSelectActivity extends BaseActivity implements HttpTaskListener, BaseRecyclerViewAdapter.OnItemClickLitener {

    public static final int FROM_WORK_NORMAL = 100;
    public static final int FROM_WORK_SPARE = 101;

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    private ServiceSelectAdapter adapter;
    private String contractId;
    private String hardwareId;
    private String ownerCompanyCode;
    private String directoryId; //判断是否 勾选
    private int from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_belongs);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        from = getIntent().getIntExtra("from", 0);

        contractId = getIntent().getStringExtra("contractId");
        hardwareId = getIntent().getStringExtra("hardwareId");
        ownerCompanyCode = getIntent().getStringExtra("ownerCompanyCode");
        directoryId = getIntent().getStringExtra("directoryId");

        simpleTileView.setTitle("选择所属目录");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ServiceSelectAdapter(ServiceSelectActivity.this, directoryId);
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);

        initData();
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        if (from ==FROM_WORK_NORMAL) {
            if ("1".equals(companyType)) {//业主
                map.put("ownerCode", ownerBean.getRows().getOwnerCode());
            } else if ("2".equals(companyType)) {//服务商
                map.put("contractId", contractId);
                map.put("hardwareId", hardwareId);
                map.put("ownerCode", ownerCompanyCode);
                map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
                map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
            }
            ServiceRequestManager.getManager().getWorkService(this, JsonMapUtil.mapToJson(map), this);
        }else if (from ==FROM_WORK_SPARE){
            map.put("contractId",contractId);
            map.put("hardwareId",hardwareId);
            ServiceRequestManager.getManager().getWorkSpareServer(this, JsonMapUtil.mapToJson(map), this);
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        OwnerContentsBean.Rows bean = adapter.getItem(position);
        Intent intent = new Intent();
        if (from==FROM_WORK_NORMAL){
            if ("1".equals(companyType)){
                intent.putExtra("directoryName", bean.getServicesDirectoryName());
                intent.putExtra("directoryId", bean.getId()+"");
            }else{
                intent.putExtra("directoryName", bean.getDetailName());
                intent.putExtra("directoryId", bean.getDetailId());
                intent.putExtra("completeTimes", bean.getCompleteTimes());
                intent.putExtra("type", bean.getType());
                intent.putExtra("relationIds", bean.getRelationId());
            }
        }else if(from ==FROM_WORK_SPARE){
            intent.putExtra("detailName", bean.getDetailName());
            intent.putExtra("relationId", bean.getRelationId());
        }
        setResult(RESULT_OK, intent);

        ServiceSelectActivity.this.finish();
    }

    @Override
    public void onSuccess(int requestId, String result) {
        OwnerContentsBean bean = new Gson().fromJson(result, OwnerContentsBean.class);
        if (bean.getMessage().getCode() == 200) {
            adapter.setData(bean.getRows());
            if (bean.getRows() != null && bean.getRows().size() != 0) {
                stateLayout.showContentView();
            } else {
                stateLayout.showEmptyView();
            }
        }
    }

    @Override    public void onException(int requestId, String code) {        showTopWrongMsg(getResources().getString(R.string.net_work_error));    }

    public static void OpenActivity(Object context, int from,String contractId, String hardwareId,
                                    String ownerCompanyCode, String directoryId, int requestCode) {
        if (context instanceof Context){
            Intent intent = new Intent((Context) context, ServiceSelectActivity.class);
            intent.putExtra("from", from);
            intent.putExtra("contractId", contractId);
            intent.putExtra("ownerCompanyCode", ownerCompanyCode);
            intent.putExtra("hardwareId", hardwareId);
            intent.putExtra("directoryId", directoryId);
            ((Activity) context).startActivityForResult(intent, requestCode);
        }else if (context instanceof Fragment){
            Intent intent = new Intent( ((Fragment) context).getContext(), ServiceSelectActivity.class);
            intent.putExtra("from", from);
            intent.putExtra("contractId", contractId);
            intent.putExtra("ownerCompanyCode", ownerCompanyCode);
            intent.putExtra("hardwareId", hardwareId);
            intent.putExtra("directoryId", directoryId);
            ((Fragment) context).startActivityForResult(intent, requestCode);
        }
    }

}
